// FontTool.cpp : Defines the entry point for the console application.
//

#include "StdAfx.h"
#include "FontTool.h"
#include "Argument.h"
#include "ximage.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		CArgument arg(argc, argv);

		LPCTSTR szOutput = arg.get("-output");
		LPCTSTR szHelp = arg.get("-help");
		if(szHelp != NULL || szOutput == NULL)
		{
			printf("Usage: -output <output path>\n\t"
				"-prefix [output file prefix, default=NULL]\n\t"
				"-fn [font face name, default=system]\n\t"
				"\t -r [font color red, default=255]\n\t"
				"\t -g [font color green, default=0]\n\t"
				"\t -b [font color blue, default=0]\n\t"
				"\t -size [font size, default=16]\n\t"
				"\t -bold [font bold, default=0]\n\t"
				"\t -italic [font italic, default=0]\n\t"
				"\t -uline [font underline, default=0]\n\t"
				"\t -help [show this Usage]\n"
				);

		}
		else
		{
			LPCTSTR szPrefix = arg.get("-prefix");
			LPCTSTR szFaceName = arg.get("-fn", "system");
			SINT r = arg.get("-r", 255);
			SINT g = arg.get("-g", 0);
			SINT b = arg.get("-b", 0);
			SINT nSize = arg.get("-size", 16);
			SINT bBold = arg.get("-bold", 0);
			SINT bItalic = arg.get("-italic", 0);
			SINT bUline = arg.get("-uline", 0);

			HFONT hFont;
			LOGFONT lFont;
			memset(&lFont, 0, sizeof(lFont));
			_tcscpy(lFont.lfFaceName, szFaceName);
			lFont.lfHeight=nSize;
			lFont.lfWeight=bBold;
			lFont.lfItalic=bItalic;
			lFont.lfUnderline=bUline;
			lFont.lfCharSet = DEFAULT_CHARSET;
			hFont = ::CreateFontIndirect(&lFont);

			COLORREF cText = RGB(r, g, b);

			CHAR c;
			if(szPrefix == NULL)
			{
				for(c = '0'; c<= '9'; c++)
				{
					CxImage xImage;
					xImage.Create(NULL, &c, 1, hFont, cText, RGB(0, 0, 0), true);

					CHAR szFile[64];
					sprintf(szFile, "%s\\d_%c.png", szOutput, c);
					xImage.Save(szFile, CXIMAGE_FORMAT_PNG);
				}
				for(c = 'a'; c<= 'z'; c++)
				{
					CxImage xImage;
					xImage.Create(NULL, &c, 1, hFont, cText, RGB(0, 0, 0), true);

					CHAR szFile[64];
					sprintf(szFile, "%s\\l_%c.png", szOutput, c);
					xImage.Save(szFile, CXIMAGE_FORMAT_PNG);
				}
				for(c = 'A'; c<= 'Z'; c++)
				{
					CxImage xImage;
					xImage.Create(NULL, &c, 1, hFont, cText, RGB(0, 0, 0), true);

					CHAR szFile[64];
					sprintf(szFile, "%s\\u_%c.png", szOutput, c);
					xImage.Save(szFile, CXIMAGE_FORMAT_PNG);
				}

				const CHAR* symbol = "*%-_!#$";
				const char* pc = symbol;
				while(*pc)
				{
					CxImage xImage;
					xImage.Create(NULL, pc, 1, hFont, cText, RGB(0, 0, 0), true);

					CHAR szFile[64];
					sprintf(szFile, "%s\\s_%d.png", szOutput, (pc-symbol));
					xImage.Save(szFile, CXIMAGE_FORMAT_PNG);
					pc++;
				}
			}
			else
			{
				// !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}
				printf("group\tFont\t.\\font\n");

				for(c='!'; c<='}'; c++)
				{
					CxImage xImage;
					TCHAR cs[4] = {0,0,0,0};
					cs[0] = c;
					cs[1] = 0;
					if(c == '&')
					{
						cs[0] = c;
						cs[1] = c;
						cs[2] = 0;

						xImage.Create(NULL, cs, 2, hFont, cText, RGB(0, 0, 0), true);
					}
					else
					{
						xImage.Create(NULL, cs, 1, hFont, cText, RGB(0, 0, 0), true);
					}

					printf("\timage\t%s%d.png\n", szPrefix, c-'!');

					CHAR szFile[64];
					sprintf(szFile, "%s\\%s%d.png", szOutput, szPrefix, c-'!');
					xImage.Save(szFile, CXIMAGE_FORMAT_PNG);

				}

				printf("sprite editor\n");
				printf("action default\n");
				printf("\tframe\n");
				for(c='!'; c<='}'; c++)
				{
					printf("\tfclip	Font\t%s%d.png 0 0 1 1 0 0 0.0 0\n", szPrefix, c-'!');
				}
			}

			::DeleteObject(hFont);

		}
	}

	return nRetCode;
}

